<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="系统类型" prop="sysType">
        <el-select v-model="queryParams.sysType" placeholder="请选择系统类型" clearable size="small">
          <el-option
            v-for="dict in sysTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="系统模块" prop="menuType">
        <el-select v-model="queryParams.menuType" placeholder="请选择系统模块" clearable size="small">
          <el-option
            v-for="dict in menuTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务id" prop="sourceId">
        <el-input
          v-model="queryParams.sourceId"
          placeholder="请输入业务id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务名称" prop="sourceName">
        <el-input
          v-model="queryParams.sourceName"
          placeholder="请输入业务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item><!--
      <el-form-item label="客户unionid" prop="unionid">
        <el-input
          v-model="queryParams.unionid"
          placeholder="请输入客户unionid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户openid" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入客户openid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信appid" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入微信appid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="客户名称" prop="custName">
        <el-input
          v-model="queryParams.custName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="custPhone">
        <el-input
          v-model="queryParams.custPhone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="导购工号" prop="fromCode">
        <el-input
          v-model="queryParams.fromCode"
          placeholder="请输入来源导购工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导购姓名" prop="fromName">
        <el-input
          v-model="queryParams.fromName"
          placeholder="请输入来源导购姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导购工号" prop="memCode">
        <el-input
          v-model="queryParams.memCode"
          placeholder="请输入分配导购工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导购姓名" prop="memName">
        <el-input
          v-model="queryParams.memName"
          placeholder="请输入分配导购姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级销区" prop="rp02name">
        <el-input
          v-model="queryParams.rp02name"
          placeholder="请输入分配导购一级销区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级销区" prop="rp03name">
        <el-input
          v-model="queryParams.rp03name"
          placeholder="请输入分配导购二级销区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="rp05name">
        <el-input
          v-model="queryParams.rp05name"
          placeholder="请输入分配导购品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分配状态" prop="memState">
        <el-select v-model="queryParams.memState" placeholder="请选择分配状态" clearable size="small">
          <el-option
            v-for="dict in memStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否跟进" prop="isFollow">
        <el-select v-model="queryParams.isFollow" placeholder="请选择是否跟进" clearable size="small">
          <el-option
            v-for="dict in isFollowOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
       <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sysCust:info:add']"
        >添加</el-button>
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sysCust:info:edit']"
        >跟进</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sysCust:info:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统类型" align="center" prop="sysType" :formatter="sysTypeFormat" width="140"/>
      <el-table-column label="系统模块" align="center" prop="menuType" :formatter="menuTypeFormat" width="120"/>
			<el-table-column label="创建时间" width="160" align="center" prop="createTime" />
      <!-- <el-table-column label="业务id" align="center" prop="sourceId" /> -->
      <el-table-column label="客户名称" align="center" prop="custName" width="140" />
      <el-table-column label="电话" align="center" prop="custPhone" width="140" :formatter="formatGePhone" />
      <el-table-column label="客户备注" align="center" prop="custRemark"width="300" />
      <el-table-column label="业务名称" align="center" prop="sourceName" width="200"/>
      <el-table-column label="分配状态" align="center" prop="memState" :formatter="memStateTypeFormat" width="120"/>
      <!-- <el-table-column label="是否跟进" align="center" prop="isFollow" :formatter="isFollowFormat" />
      <el-table-column label="跟进内容" align="center" prop="followConent" width="300" />
      <el-table-column label="客户unionid" align="center" prop="unionid" width="260"/>
      <el-table-column label="客户openid" align="center" prop="openId" />
      <el-table-column label="微信appid" align="center" prop="appId" /> -->
      <!-- <el-table-column label="客户头像" align="center" prop="headimgurl" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="国家" align="center" prop="country" />-->
      <el-table-column label="分配导购工号" align="center" prop="memCode" width="120" />
      <el-table-column label="分配导购姓名" align="center" prop="memName" width="120" />
      <el-table-column label="分配导购一级销区" align="center" prop="rp02name" width="120" />
      <el-table-column label="分配导购二级销区" align="center" prop="rp03name" width="120" />
      <el-table-column label="分配导购品牌" align="center" prop="rp05name" width="120" />
      <el-table-column label="来源导购工号" align="center" prop="fromCode" width="120" />
      <el-table-column label="来源导购姓名" align="center" prop="fromName" width="120" />
      <el-table-column label="来源外部人员id" align="center" prop="wbUserId" width="120" />
      <el-table-column label="来源外部人员姓名" align="center" prop="wbUserName" width="120" />
      <el-table-column label="微客客户号" align="center" prop="crmCustId" width="90" />
      <el-table-column label="客户姓名" align="center" prop="cName" width="140" />
      <el-table-column label="客户电话" align="center" prop="cPhone" width="140" :formatter="formatGePhone" />
      <el-table-column label="客户状态" align="center" prop="currStatus" />
      <el-table-column label="跟进次数" align="center" prop="followTimes" />
      <el-table-column label="一级销区" align="center" prop="attribute5" />
      <el-table-column label="二级销区" align="center" prop="attribute7" />
      <el-table-column label="产品部" align="center" prop="attribute9" />
      <el-table-column label="门店" align="center" prop="deptName" width="180" />
      <el-table-column label="微客所属人" align="center" prop="createCode" width="110" />
      <el-table-column label="微客所属人姓名" align="center" prop="createName" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sysCust:info:edit']"
          >跟进</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加客户信息标准接口对话框-->
    <el-dialog :title="title" :visible.sync="addOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户名称" prop="custName">
          <el-input v-model="form.custName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="电话" prop="custPhone">
          <el-input v-model="form.custPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="客户备注" prop="custRemark">
          <el-input  type="textarea" :rows="2" v-model="form.custRemark" placeholder="请输入客户备注" />
        </el-form-item>
        <el-form-item label="分配导购工号" prop="memCode">
          <el-input v-model="form.memCode" placeholder="请输入分配导购工号" />
        </el-form-item>
        <el-form-item label="分配导购姓名" prop="memName">
          <el-input v-model="form.memName" placeholder="请输入分配导购姓名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addSubmitForm">确 定</el-button>
        <el-button @click="addCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改客户信息标准接口对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="是否有效" prop="isActive">
          <el-input v-model="form.isActive" placeholder="请输入是否有效" />
        </el-form-item>
        <el-form-item label="租户id" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="系统类型">
          <el-select v-model="form.sysType" placeholder="请选择系统类型">
            <el-option
              v-for="dict in sysTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系统名称" prop="sysName">
          <el-input v-model="form.sysName" placeholder="请输入系统名称" />
        </el-form-item>
        <el-form-item label="业务id" prop="sourceId">
          <el-input v-model="form.sourceId" placeholder="请输入业务id" />
        </el-form-item>
        <el-form-item label="业务名称" prop="sourceName">
          <el-input v-model="form.sourceName" placeholder="请输入业务名称" />
        </el-form-item>
        <el-form-item label="客户unionid" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入客户unionid" />
        </el-form-item>
        <el-form-item label="客户openid" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入客户openid" />
        </el-form-item>
        <el-form-item label="微信appid" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入微信appid" />
        </el-form-item>
        <el-form-item label="客户名称" prop="custName">
          <el-input v-model="form.custName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户头像" prop="headimgurl">
          <el-input v-model="form.headimgurl" placeholder="请输入客户头像" />
        </el-form-item>
        <el-form-item label="电话" prop="custPhone">
          <el-input v-model="form.custPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="来源导购工号" prop="memCode">
          <el-input v-model="form.memCode" placeholder="请输入来源导购工号" />
        </el-form-item>
        <el-form-item label="来源导购姓名" prop="memName">
          <el-input v-model="form.memName" placeholder="请输入来源导购姓名" />
        </el-form-item> -->
        <el-form-item label="是否跟进">
          <el-select v-model="form.isFollow" placeholder="请选择是否跟进">
            <el-option
              v-for="dict in isFollowOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进内容" prop="followConent">
          <el-input  type="textarea" :rows="2"  v-model="form.followConent" placeholder="请输入跟进内容" />
        </el-form-item>
        <el-form-item label="分配状态">
          <el-select v-model="form.memState" placeholder="请选择分配状态">
            <el-option
              v-for="dict in memStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
              :disabled="dict.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分配导购工号" prop="memCode">
          <el-input v-model="form.memCode" placeholder="请输入分配导购工号" />
        </el-form-item>
        <el-form-item label="分配导购姓名" prop="memName">
          <el-input v-model="form.memName" placeholder="请输入分配导购姓名" />
        </el-form-item>
        <el-form-item label="客户备注" prop="custRemark">
          <el-input  type="textarea" :rows="2"  v-model="form.custRemark" placeholder="请输入客户备注" />
        </el-form-item>
        <el-form-item label="备注" prop="sysRemark">
          <el-input v-model="form.sysRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, addApiNew, updateInfo, exportInfo } from "@/api/sysCust/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 客户信息标准接口表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addOpen: false,
      // 系统类型字典
      sysTypeOptions: [],
      // 系统类型字典
      menuTypeOptions: [],
      // 是否跟进字典
      isFollowOptions: [],
      //分配状态字典
      memStateOptions: [{
                dictValue: '0',
                dictLabel: ' 未分配',
         //       disabled: true
              }, {
                dictValue: '1',
                dictLabel: '已分配',
          //      disabled: true
              }, {
                dictValue: '2',
                dictLabel: '无需分配（已录入）'
              }, {
                dictValue: '3',
                dictLabel: '无需分配（未开通）'
              }, {
                dictValue: '4',
                dictLabel: '无需分配（客户无需求）'
              }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isActive: undefined,
        lesseeId: undefined,
        sysType: undefined,
        sysName: undefined,
        sourceId: undefined,
        sourceName: undefined,
        unionid: undefined,
        openId: undefined,
        appId: undefined,
        custName: undefined,
        headimgurl: undefined,
        custPhone: undefined,
        province: undefined,
        city: undefined,
        country: undefined,
        memCode: undefined,
        memName: undefined,
        fromCode: undefined,
        fromName: undefined,
        isFollow: undefined,
        followConent: undefined,
        custRemark: undefined,
        sysRemark: undefined,
        memState: undefined,
        rp02name: undefined,
        rp03name: undefined,
        rp05name: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        custPhone: [
                { required: true, message: '请输入电话', trigger: 'blur' }
              ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("SYS_SOURCE_TYPE").then(response => {
      this.sysTypeOptions = response.data;
    });
    this.getDicts("SYS_SOURCE_TYPE_MENU").then(response => {
      this.menuTypeOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isFollowOptions = response.data;
    });
  },
  methods: {
    /** 查询客户信息标准接口列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 系统类型字典翻译
    sysTypeFormat(row, column) {
      return this.selectDictLabel(this.sysTypeOptions, row.sysType);
    },
    memStateTypeFormat(row, column) {
      return this.selectDictLabel(this.memStateOptions, row.memState);
    },
    // 系统类型字典翻译
    menuTypeFormat(row, column) {
      return this.selectDictLabel(this.menuTypeOptions, row.menuType);
    },
    // 是否跟进字典翻译
    isFollowFormat(row, column) {
      return this.selectDictLabel(this.isFollowOptions, row.isFollow);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    addCancel() {
      this.addOpen = false;
      this.reset();
    },
    formatGePhone(row) {
      if (row.custPhone) {
        return row.custPhone.substring(0, 3)+"****"+row.custPhone.substr(row.custPhone.length-4);
      }
      if (row.custPhone) {
        return row.cPhone.substring(0, 3)+"****"+row.cPhone.substr(row.cPhone.length-4);
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: this.$store.state.user.name || '',
        createTime: undefined,
        updateBy: this.$store.state.user.name || '',
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        sysType: undefined,
        sysName: undefined,
        menuType: undefined,
        menuName: undefined,
        sourceId: undefined,
        sourceName: undefined,
        unionid: undefined,
        openId: undefined,
        appId: undefined,
        custName: undefined,
        headimgurl: undefined,
        custPhone: undefined,
        province: undefined,
        city: undefined,
        country: undefined,
        memCode: undefined,
        memName: undefined,
        isFollow: undefined,
        followConent: undefined,
        custRemark: undefined,
        sysRemark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addOpen = true;
      this.title = "添加新客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "跟进客户";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户信息标准接口编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户信息标准接口数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 添加提交按钮 */
    addSubmitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysType='SYS_SOURCE_PT'
          this.form.sysName='平台'
          this.form.menuType='SYS_SOURCE_TYPE_MENU_PT_ADD'
          this.form.menuName='平台新增'
          this.form.isTocrm='1'
          this.form.sourceId=''
          this.form.sourceName='无'
          this.form.fromCode=this.$store.state.user.name || ''
            addApiNew(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
        }
      });
    },
  }
};
</script>
